package com.sam.im.heiqun.uis.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.MyCircleItem;
import com.sam.im.heiqun.entities.MyGiftItem;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.GiftSendAdapter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangguodong on 2017/9/18.
 */

public class GiftListActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.txt_gift)
    TextView txt_gift;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;

    private PGService pgservice;

    private GiftSendAdapter giftSendAdapter ;

    private List<MyGiftItem.ListBean> datas = new ArrayList<>();

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.my_gift);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        pgservice = PGService.getInstance();

        giftSendAdapter = new GiftSendAdapter(GiftListActivity.this,datas);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(giftSendAdapter);

        inidata(0);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_gift_list;
    }


    private void inidata(final int pageNo) {

        pgservice.myGift(""+pageNo, ToolsUtils.getMyUserId()).subscribe(new AbsAPICallback<MyGiftItem>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast(getResources().getString(R.string.get_data_fail));
            }

            @Override
            public void onNext(final MyGiftItem myCircleItem) {
                if (myCircleItem != null) {
//                    if (myCircleItem.getList().size() > 0) {
                        if (0 == pageNo)datas.clear();
                        int start = datas.size();
                        datas.addAll(myCircleItem.getList());
//                        giftSendAdapter.notifyItemRangeInserted(start, myCircleItem.getList().size());
                        giftSendAdapter.notifyDataSetChanged();
//                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            txt_gift.setText(getResources().getString(R.string.now_gift_num)+myCircleItem.getTotalCount()+getResources().getString(R.string.num));
                        }
                    });
                }

            }
        });
    }

}
